<?php
/*
	Copyright (C) 2008 - 2012 Inanta Martsanto
	Inanta Martsanto (inanta@inationsoft.com)

	This file is part of NewStep Framework.

	NewStep Framework is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	NewStep Framework is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with NewStep Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Inationsoft\NS;

/**
 *Handle function to manipulate string
 *
 *@author Inanta Martsanto <inanta@inationsoft.com>
 */
class String extends Object {
	static function limitCharacter($string, $limit, $end_string = '...') {
		return (@strlen($string) > $limit ? @substr($string, 0, $limit) . $end_string : $string);
	}

	static function limitWord($string, $limit, $end_string = '...') {
		if(@count($word = @explode(' ', $string, $limit + 1)) > $limit) {
			$string = @implode(' ', @array_slice($word, 0, $limit)) . $end_string;
		}

		return $string;
	}

	static function toSafeURI($str, $replace=array(), $delimiter='-') {
		if( !empty($replace) ) {
			$str = @str_replace((array)$replace, ' ', $str);
		}

	       $clean = @iconv('UTF-8', 'ASCII//TRANSLIT', $str);
	       $clean = @preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
	       $clean = @strtolower(trim($clean, '-'));
	       $clean = @preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

	       return $clean;
	}

	static function toLowerCase($str) {
		return @strtolower($str);
	}

	static function toUpperCase($str) {
		return @strtoupper($str);
	}

	static function toTitleCase($str) {
		return @ucwords($str);
	}

	static function toProperCase($str) {
		return @ucwords($str);
	}

	static function toSentenceCase($str) {
		return @ucfirst($str);
	}

	/**
	*Invert character case
	*
	*/
	static function invertCase($str) {
		return @strtolower($str) ^ @strtoupper($str) ^ $str;
	}

	static function contains($string, $search) {
		if(!is_array($search)) $search = array($search);

		foreach($search as $item) {
			if(@preg_match('/' . $item . '/', $string) == 1) {
				return true;
			}
		}

		return false;
	}

	static function endsWith($string, $search) {
		if(!is_array($search)) $search = array($search);

		foreach($search as $item) {
			if(@preg_match('/' . $item . '$/', $string) == 1) {
				return true;
			}
		}

		return false;
	}

	static function startsWith($string, $search) {
		if(!is_array($search)) $search = array($search);

		foreach($search as $item) {
			if(@preg_match('/^' . $item . '/', $string) == 1) {
				return true;
			}
		}

		return false;
	}

	static function join($strings, $glue = ' ') {
		return @implode($glue, $strings);
	}

	static function concat() {
		return @implode('', func_get_args());
	}

	static function replace($search, $replace, $string) {
		return @str_replace($search, $replace, $string);
	}

	static function indexOf($string, $search) {
		return @strpos($string, $search);
	}

	static function padLeft($str, $length, $pad = ' ') {
		return str_pad($str, $length, $pad, STR_PAD_LEFT);
	}

	static function padRight($str, $length, $pad = ' ') {
		return str_pad($str, $length, $pad, STR_PAD_RIGHT);
	}
}
?>